package com.apache.client;

import java.util.Map;

import com.apache.api.vo.ResultEntity;
import com.apache.rpc.client.RpcProtoclClient;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

/**
 * description:  
 * @author mhguo 创建时间：2015-10-20  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class DictCoreClient {

	private static DictCoreClient instance;
	private static final String serviceName = "dictService";
	private String sysPass = "dwusoft2016";

	private DictCoreClient() {
		sysPass = StrUtil.doNull(ToolsUtil.getInstance().getClientValueByKey("push_to_dwusoft_pass"), "dwusoft2016");
	}

	public static DictCoreClient getInstance() {
		if (null == instance) {
			instance = new DictCoreClient();
		}
		return (instance);
	}

	public ResultEntity dictSelect(Map<String, Object> dataMap) {
		dataMap.put("sysPass", sysPass);
		ResultEntity root = RpcProtoclClient.getInstance().doService(serviceName, "dictSelect", dataMap,
				getInterfaceRegister());
		return root;
	}

	/**
	 * description:  获取地区信息
	 */
	@SuppressWarnings("rawtypes")
	public Map dictCityInfo(Map<String, Object> dataMap) {
		dataMap.put("sysPass", sysPass);
		ResultEntity root = RpcProtoclClient.getInstance().doService(serviceName, "cityList", dataMap,
				getInterfaceRegister());
		if ("true".equals(root.getResult())) {
			return (Map) root.getEntity();
		}
		return null;
	}

	public Object dictItemInfo(Map<String, Object> dataMap) {
		dataMap.put("sysPass", sysPass);
		ResultEntity root = RpcProtoclClient.getInstance().doService(serviceName, "viewDictItem", dataMap,
				getInterfaceRegister());
		if ("true".equals(root.getResult())) {
			return root.getEntity();
		}
		return null;
	}

	public ResultEntity dictList(Map<String, Object> dataMap) {
		dataMap.put("sysPass", sysPass);
		ResultEntity root = RpcProtoclClient.getInstance().doService(serviceName, "dictList", dataMap,
				getInterfaceRegister());
		return root;
	}

	private InterfaceRegister getInterfaceRegister() {
		InterfaceRegister register = new InterfaceRegister();
		String reqUrl = ToolsUtil.getInstance().getValueByKey("dictServer_ip");
		String infoUrl = reqUrl + "/dict/service/doService.action";
		if (reqUrl.startsWith("https:")) {
			register.setCallType("https");
			register.setAddress(infoUrl);
		} else if (reqUrl.startsWith("http:")) {
			register.setCallType("http");
			register.setAddress(infoUrl);
		} else {
			String port = ToolsUtil.getInstance().getValueByKey("dictServer_port");
			register.setAddress(reqUrl);
			register.setPort(port);
			register.setCallType("socket");
		}
		return register;
	}
}
